package org.yak.core.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * json格式转换
 *
 * @平台：统一开发平台
 * @文件名称：JsonUtils.java
 * @文件描述：
 * @author xuqiang
 * @日期：2018年6月15日上午8:57:43
 *
 */
public class JsonUtils {

	private static final Logger logger = Logger.getLogger(JsonUtils.class);
	
	/**
	 * json字符串转换map,默认key全部转换成大写
	 * @param jsonParam
	 * @return
	 */
	public static Map<String,Object> toMap(String jsonParam){
		JSONObject json = JSONObject.fromObject(jsonParam);
		return toMap(json,true);
	}
	
	/**
	 * json字符串转换map
	 * @param jsonParam
	 * @param isMapKeyUpperCase map中的key是否大写
	 * @return
	 */
	public static Map<String,Object> toMap(String jsonParam,boolean isMapKeyUpperCase){
		JSONObject json = JSONObject.fromObject(jsonParam);
		return toMap(json,isMapKeyUpperCase);
	}
	
	/**
	 * json对象转换map
	 * @param json
	 * @param isMapKeyUpperCase map中的key是否大写
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> toMap(JSONObject json,boolean isMapKeyUpperCase){
		Map<String, Object> map = new HashMap<String,Object>();
		for (Object k : json.keySet()) {
			Object v = json.get(k);
			if (v instanceof JSONArray) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				Iterator<JSONObject> it = ((JSONArray) v).iterator();
				while (it.hasNext()) {
					JSONObject json2 = it.next();
					list.add(toMap(json2.toString()));
				}
				if(isMapKeyUpperCase) {
					//大写
					map.put(k.toString().toUpperCase(), list);
				}else {
					//默认
					map.put(k.toString(), list);
				}
			} else {
				String key = "";
				if(isMapKeyUpperCase) {
					//大写
					key = k.toString().toUpperCase();
				}else {
					//默认
					key = k.toString();
				}
				if(StringUtils.isEmpty(v)){
					map.put(key, null);
				}else{
					map.put(key, v);
				}
				
			}
		}
		return map;
	}
	
	/**
	 * 获取单个JSONObject对象的key
	 * @param json
	 * @return
	 */
	public static String getSingJsonKey(JSONObject json){
		Iterator<?> it = json.keys();
		String key = "";
		while (it.hasNext()) {
			key = it.next().toString();
		}
		return key;
	}
	
	/**
	 * json转class类
	 * @param jsonStr
	 * @param clazz
	 * @return
	 * @throws InstantiationException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public static <T> T toClass(String jsonStr,Class<T> clazz){
		try {
			JSONObject jsonObj = JSONObject.fromObject(jsonStr);
			Map<String,Object> valueMap = toMap(jsonObj,false);
			T obj = clazz.newInstance();
			BeanUtils.populate(obj, valueMap);
			return obj;
		}catch(Exception e) {
			logger.error("json转换对象异常",e);
			return null;
		}
	}
}
